package cn.kaguyaever.dao.impl;

import cn.kaguyaever.dao.VoteDao;
import cn.kaguyaever.entity.Vote;

import java.util.List;

public class VoteDaoImpl extends BaseDao<Vote> implements VoteDao {
    public int createVote(Vote vote){
        String sql = "insert into vote(`voteId`,`title`,`projectId`,`winner`,`voteStatus`,`num`) values(?,?,?,?,?,?)";
        return  update(sql,vote.getVoteId(),vote.getName(),vote.getP(),vote.getWinner(),vote.getVoteStatus(),vote.getNum());
    }

    public Vote queryVoteByVoteId(String voteId){
        String sql = "select `voteId`,`title`,`projectId`,`winner`,`voteStatus`,`num` from vote where voteId = ?";
        return queryForOne(sql,voteId);
    }

    public List<Vote> queryAllVoteByProjectId(String projectId){
        String sql = "select `voteId`,`title`,`projectId`,`winner`,`voteStatus`,`num` from vote where projectId = ?";
        return queryForList(sql,projectId);
    }

    public int beginVote(String voteId){
        String sql = "update vote set voteStatus = 1 where voteId = ?";
        return update(sql,voteId);
    }

    public int endVote(String voteId){
        String sql = "update vote set voteStatus = 2 where voteId = ?";
        return update(sql,voteId);
    }

    public int setWinner(String winner,String voteId){
        String sql = "update vote set winner = ? where voteId = ?";
        return update(sql,winner,voteId);
    }

    public int deleteVote(String projectId){
        String sql = "delete from vote where projectId = ?";
        return update(sql,projectId);
    }
}
